﻿using OvenParameterForm.Model;
using OvenParameterForm.Util;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OvenParameterForm.CustomizeControl
{
    public partial class Tc_OvenHistoryChart : UserControl
    {
        UILineSeries series1;
        UILineSeries series2;
        UILineSeries series3;
        UILineSeries series4;
        UILineSeries series5;
        UILineOption option;
        private SqlSugarClientHelp<OvenTemperatureChart> sqlChartDAL = new SqlSugarClientHelp<OvenTemperatureChart>();
        public Tc_OvenHistoryChart(string name)
        {
            InitializeComponent();
            this.Name = name;
            LoadPage();
        }

        public void LoadPage()
        {

            this.lbx_chartList.Items.Clear();
            option = new UILineOption();
            option.ToolTip.Visible = true;
            option.Title = new UITitle();
            option.Title.Text = "实时数据曲线";
            option.Title.SubText = "温度与真空度曲线";

            option.XAxisType = UIAxisType.DateTime;
            series1 = option.AddSeries(new UILineSeries("层1温度曲线", Color.Red));
            series2 = option.AddSeries(new UILineSeries("层2温度曲线", Color.Green));
            series3 = option.AddSeries(new UILineSeries("层3温度曲线", Color.Orange));
            series4 = option.AddSeries(new UILineSeries("层4温度曲线", Color.Blue));
            series5 = option.AddSeries(new UILineSeries("真空度", Color.Purple, true));

            series1.Symbol = UILinePointSymbol.None;
            series1.SymbolSize = 3;
            series1.SymbolLineWidth = 2;
            series1.Smooth = true;
            series1.Color = Color.Red;
            series1.ShowLine = true;

            series2.Symbol = UILinePointSymbol.None;
            series2.SymbolSize = 3;
            series2.SymbolLineWidth = 2;
            series2.Smooth = true;
            series2.Color = Color.Green;
            series2.ShowLine = true;

            series3.Symbol = UILinePointSymbol.None;
            series3.SymbolSize = 3;
            series3.SymbolLineWidth = 2;
            series3.Smooth = true;
            series3.Color = Color.Orange;
            series3.ShowLine = true;

            series4.Symbol = UILinePointSymbol.None;
            series4.SymbolSize = 3;
            series4.SymbolLineWidth = 2;
            series4.Smooth = true;
            series4.Color = Color.Blue;
            series4.ShowLine = true;

            series5.Symbol = UILinePointSymbol.None;
            series5.SymbolSize = 3;
            series5.SymbolLineWidth = 2;
            series5.Smooth = true;
            series5.Color = Color.Purple;
            series5.ShowLine = true;

            option.XAxis.Name = "时间";//横轴名称
            option.YAxis.Name = "摄氏度";//纵轴1名称
            option.Y2Axis.Name = "真空度";//纵轴2名称
            option.XAxis.AxisLabel.DateTimeFormat = "HH:mm:ss";
            uiLineChart1.SetOption(option);

            lbx_chartList.Items.Clear();
            var v = sqlChartDAL._db.Queryable<OvenTemperatureChart>().Where(w => w.OvenName == this.Name).Distinct().Select(it => it.ChartName).ToList();
            v.ForEach(f => lbx_chartList.Items.Add(f));


        }

        private void lbx_chartList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbx_chartList.SelectedItem != null)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                series1.Clear();
                series2.Clear();
                series3.Clear();
                series4.Clear();
                series5.Clear();
                string tempname = lbx_chartList.SelectedItem.ToString();
                Stopwatch sw1 = new Stopwatch();
                sw1.Start();
                List<OvenTemperatureChart> templist = sqlChartDAL.QueryAll().Where(w => w.OvenName.Equals(this.Name) && w.ChartName.Equals(tempname)).OrderBy(o => o.ChartXData).ToList();
                sw1.Stop();
                templist.ForEach(t =>
                {
                    series1.Add(t.ChartXData, t.ChartY1Data);
                    series2.Add(t.ChartXData, t.ChartY2Data);
                    series3.Add(t.ChartXData, t.ChartY3Data);
                    series4.Add(t.ChartXData, t.ChartY4Data);
                    series5.Add(t.ChartXData, t.ChartY5Data);
                });
                Console.WriteLine($"查询数据库耗时{sw1.ElapsedMilliseconds}毫秒");

                option.XAxis.Name = "时间";//横轴名称
                option.YAxis.Name = "摄氏度";//纵轴1名称
                option.Y2Axis.Name = "真空度";//纵轴2名称
                option.XAxis.AxisLabel.DateTimeFormat = "HH:mm:ss";
                option.XAxis.SetMinValue(DateTime.Parse("00:00:01"));
                //   Console.WriteLine($"任务状态{t1.Status}");
                // List<OvenTemperatureChart> templist = await t1;

                uiLineChart1.SetOption(option);

                sw.Stop();
                Console.WriteLine($"打开曲线共耗时{sw.ElapsedMilliseconds}毫秒");
            }
        }
    }
}
